<?php

namespace app\index\controller;

class Order extends Base
{
    /**
     * 显示订单列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $map = [];
        if (input('param.shop_id')) {
            if (! $this->user['is_shop']) {
                return $this->error('您不是商家身份');
            }
            $map['shop_id'] = input('param.shop_id');
        } else {
            $map['uid'] = $this->user['uid'];
        }

        if (is_numeric(input('param.status'))) {
            $map['status'] = input('param.status');
        }

        if (is_numeric(input('param.is_pay'))) {
            $map['is_pay'] = input('param.is_pay');
        }

        $list = model('Order')->getList($map, input('param.size'));
        $this->result($list);
    }

    /**
     * 生成订单
     *
     * @return \think\Response
     */
    public function create()
    {
        $post = input('post.');
        $rst = model('Order')->addInfo($post, $this->user['uid']);
        if (is_numeric($rst)) {
            $this->result(['orderid' => $rst]);
        }
        $this->success($rst);
    }

    /**
     * 获取订单详情
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read()
    {
        
        $oid = input('param.orderid');

        $info = model('Order')->getInfo($oid);

        $this->result($info);
    }

    // 支付订单
    public function payOrder()
    {
        $post = input('post.');
        $rst = model('Order')->pay($post['orderid'], $post['note']);

        $this->success($rst);
    }

    // 取消未支付订单
    public function cancleOrder()
    {
        $orderid = input('post.id');

        $rst = model('Order')->cancleOrder($orderid);

        $this->success($rst);
    }

    // 确认收货
    public function confirmOrder()
    {
        $orderid = input('post.id');

        $rst = model('Order')->confirm($orderid, $this->user['name']);

        $this->success($rst);
    }
}
